package com.fy.market.types.common.reslut;

import lombok.Data;

import java.io.Serializable;

/**
 * 响应对象
 * @author fwq
 * @date 2025-07-24 10:00
 */
@Data
public class R<T> implements Serializable {
    private static final Integer OK_CODE = 200;
    private static final Integer NO_CODE = 400;
    private static final String OK = "请求成功";
    private static final String NO = "请求失败";
    private Integer code;
    private String msg;
    private T data;

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> ok() {
        return new R<>(OK_CODE , OK, null);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(OK_CODE , OK, data);
    }

    public static <T> R<T> no() {
        return new R<>(NO_CODE , NO, null);
    }

    public static <T> R<T> no(String msg) {
        return new R<>(NO_CODE , msg, null);
    }

    /**
     * 创建一个自定义 code 和 msg 的响应对象
     * @param code 自定义的响应码
     * @param msg 自定义的响应消息
     * @param data 响应数据
     * @param <T> 数据类型
     * @return 自定义的响应对象
     */
    public static <T> R<T> custom(int code, String msg, T data) {
        return new R<>(code, msg, data);
    }
}